﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using ANZ_AIM_DAL;
using System.Globalization;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_BLL.Utils;
using ANZ_AIM_FRAMEWORK;

namespace ANZ_AIM_BLL
{
    public class SaleSQMRawDataBLL
    {
        private string ConnectionString;
        private const string TABLE_NAME = "SaleSQMRawData";
        private Dictionary<string, string> ObjectPropDict;

        public SaleSQMRawDataBLL()
        {
            ConnectionString = DBHelper.GetMainMdbConnectionString();
            this.ObjectPropDict = BuildObjectPropertyMapping();
        }

        public void SaveSQMRawData(List<SaleSQMRawData> saleSQMRawDataList)
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                OleDbParameter[] parameters;
                string command = string.Empty;
                foreach (SaleSQMRawData saleSQMRawData in saleSQMRawDataList)
                {
                    command = CommandHelper.BuildInsertCommand(saleSQMRawData, dict, TABLE_NAME, out parameters);
                    OleDbHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, command, parameters);
                }
            }
            catch (Exception ex) { throw; }
        }

        public DataTable GetBySaleCodeAndMonth(string saleCode, string batchFileSaleInfoID)
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                SaleSQMRawData saleSQMRawData = new SaleSQMRawData() { Agent = saleCode, BatchFileSaleInfoID=batchFileSaleInfoID };
                OleDbParameter[] parameters;
                var conditionDict = new Dictionary<string, string>
                    {
                        {"Agent", "Agent"},
                        {"BatchFileSaleInfoID", "BatchFileSaleInfoID"}
                    };
                string command = CommandHelper.BuildSelectCommand(saleSQMRawData, dict, conditionDict, out parameters);
                DataSet dsResult = OleDbHelper.ExecuteDataset(this.ConnectionString, CommandType.Text, command, parameters);

                return dsResult.Tables[0];
            }
            catch (Exception ex) { throw; }
        }

        public Dictionary<string, string> BuildObjectPropertyMapping()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>
            {
                {"Month", "Month"},
                {"ProductType", "ProductType"},
                {"ApprovedLimit", "ApprovedLimit"},
                {"Type", "Type"},
                {"Agent", "Agent"},
                {"SaleName", "SaleName"},
                {"Team", "Team"},
                {"Role", "Role"},
                {"Incidents", "Incidents"},
                {"IncidentCode", "IncidentCode"},
                {"RiskPoint", "RiskPoint"},
                {"Channel", "Channel"},
                {"SourceCode", "SourceCode"},
                {"FullName", "FullName"},
                {"SubmitDate", "SubmitDate"},
                {"Product", "Product"},
                {"OpenDate", "OpenDate"},
                {"SQMMonth", "SQMMonth"},
                {"BatchFileSaleInfoID", "BatchFileSaleInfoID"},   
                {"ProfileID", "ProfileID"} 
            };

            return dictionary;
        }
    }
}
